
package com.sgcc.sx.pmos.common.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.common.adapter.ChartMakerAdapter;
import com.sgcc.sx.pmos.common.base.ChartMakerBean;
import com.sgcc.sx.pmos.common.utils.BigDecimalUtils;

import java.util.ArrayList;
import java.util.List;

public class LineMarkerView1 extends com.github.mikephil.charting.components.MarkerView {

    private final TextView tvContent;
    private final RecyclerView rvList;
    private String[] xText;
    private List<List<String>> dataList;
    private Context context;
    private int type;
    private int[] colors = new int[]{Color.parseColor("#FFffa800"),
            Color.parseColor("#FF0092FF"), Color.parseColor("#FFAA84F4")};
    private int[] colors1 = new int[]{Color.parseColor("#FF0092FF"),
            Color.parseColor("#FF2BC98E")};

    private int[] colors2 = new int[]{Color.parseColor("#FF0092FF"),
            Color.parseColor("#FFffa800")};

    public LineMarkerView1(Context context, String[] xText, List<List<String>> dataList, int type) {
        super(context, R.layout.custom_marker_view1);
        this.context = context;
        this.xText = xText;
        this.type = type;
        this.dataList = dataList;
        tvContent = findViewById(R.id.tvContent);
        rvList = findViewById(R.id.rvList);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        switch (type) {
            default:
                if (e instanceof CandleEntry) {
                    CandleEntry ce = (CandleEntry) e;
                    setRvList((int) ce.getX());
                    tvContent.setText(xText[(int) ce.getX()]);
                } else {
                    setRvList((int) e.getX());
                    tvContent.setText(xText[(int) e.getX()]);
                }

                break;
            case 2:

                if (e instanceof CandleEntry) {
                    CandleEntry ce = (CandleEntry) e;
                    setRvList((int) ce.getX());
                    tvContent.setText("时段" + xText[(int) ce.getX()]);
                } else {
                    setRvList((int) e.getX());
                    tvContent.setText("时段" + xText[(int) e.getX()]);
                }

                break;
            case 3:
            case 4:
                if (e instanceof CandleEntry) {
                    CandleEntry ce = (CandleEntry) e;
                    setRvList((int) ce.getX());
                    tvContent.setText(xText[(int) ce.getX()] + "月");
                } else {
                    setRvList((int) e.getX());
                    tvContent.setText(xText[(int) e.getX()] + "月");
                }

                break;


        }

        super.refreshContent(e, highlight);
    }

    private void setRvList(int index) {
        List<ChartMakerBean> chartMakerBeanList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            String value;
            String key;
            if (type == 1 && i == 0) {
                List<String> strings = dataList.get(i);
                if (null != strings && strings.size() > 0)
                    value = BigDecimalUtils.onKeep2Decimal_new(strings.get(index)) + "MWh";
                else
                    value = "";
                key = "1";
            } else {
                List<String> strings = dataList.get(i);
                if (null != strings && strings.size() > 0)
                    value = BigDecimalUtils.onKeep2Decimal_new(strings.get(index)) + "元/MWh";
                else
                    value = "";
                key = "0";
            }
            if (!TextUtils.isEmpty(value))
                chartMakerBeanList.add(new ChartMakerBean(type == 1 ? colors1[i] : type == 3 ? colors2[i] : colors[i], key, value));
        }

        rvList.setLayoutManager(new LinearLayoutManager(context));
        rvList.setAdapter(new ChartMakerAdapter(chartMakerBeanList));


    }


    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
